package com.ztman.rxjava.mvp.view;


import android.app.Activity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ztman.rxjava.utils.ToastPop;

import butterknife.ButterKnife;

/**
 * Created by YJH on 2017/11/30.
 * 功能：实现接口-IBaseViewActivity
 * 为子类activity提供预制方法和初始化流程的基类
 * 视图层代理的基类
 */

public abstract class BaseViewActivityIml implements IBaseViewActivity{
    //存储 view控件的对象
    protected final SparseArray<View> mViews = new SparseArray<>();
    protected View mRootViw;

    public abstract int getLayoutIdInBaseView();

    @Override
    public void createInIBaseView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int rootLayoutId = getLayoutIdInBaseView();

        mRootViw = inflater.inflate(rootLayoutId, container, false);
        ButterKnife.bind(this, mRootViw);

    }


    @Override
    public View getRootViewInIBaseView() {
        return mRootViw;
    }

    public void setRootViewInBaseView(View rootView){
        mRootViw = rootView;
    }

    @Override
    public void InitWidgetInIBaseView() {

    }



    //短土司
    public void showToastShort(String msg){
        ToastPop.showShort(msg);
    }
    //长土司
    public void showToastLong(String msg){
        ToastPop.showLong(msg);
    }

    /**
     * 功能：通过View 获取对应的当前视图View的Activity对象
     * @param <T>
     * @return
     */
    public <T extends Activity> T getActivityInBaseView(){
        return (T)mRootViw.getContext();
    }

    /**
     * 功能：根据view的id获取view对象
     * @param id
     * @param <T>
     * @return
     */
    private <T extends View> T getViewInBaseView(int id){
        return (T)bindView(id);
    }
    private  <T extends View> T bindView(int resId){
        T view = (T) mViews.get(resId);
        if(null == view){
            view = (T) mRootViw.findViewById(resId);
            mViews.put(resId, view);
        }

        return view;
    }

    /**
     * 功能：设置控件的监听
     * @param listener
     * @param ids
     */
    public void setOnClickListener(View.OnClickListener listener, int... ids) {
        if (ids == null) {
            return;
        }
        for (int id : ids) {
            getViewInBaseView(id).setOnClickListener(listener);
        }
    }
}
